﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Cms.Domain;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Cms.Tests.Unit.Domain
{

    [TestClass]
    public class MasterTemplateTests
    {

        [TestMethod]
        public void TestGetAndSetCode()
        {
            // Test code behind setter and getter
            MasterTemplate masterTemplate = new MasterTemplate();
            Assert.AreEqual("", masterTemplate.GetCode());
            Administrator admin = new Administrator();
            masterTemplate.SetCode("some code", admin);
            Assert.AreEqual("some code", masterTemplate.GetCode());
            Assert.AreEqual(1, masterTemplate.SourceCode.Versions.Count);
        }


        [TestMethod]
        public void GetPublishedContentHtmlAndTitle_AreaAndContentExists_Success()
        {
            MasterTemplate masterTemplate = new MasterTemplate();
            Content c = new Content();
            Locale locale = new Locale { ID = 1 };
            Administrator admin = new Administrator() { ID = 1 };
            MasterTemplateContentLink masterTemplateLink = new MasterTemplateContentLink
                                                               {
                                                                   Content = c, 
                                                                   MasterTemplate = masterTemplate, 
                                                                   AreaID = "mtcHeader"
                                                               };
            masterTemplate.Content.Add(masterTemplateLink);
            ContentTranslationVersion ctv = c.AddVersion(locale, DateTime.Now.Ticks, "some html", "title", admin);
            ctv.Translation.PublishedVersion = ctv;
            Assert.AreEqual("some html", masterTemplate.GetPublishedContentHtml("mtcHeader", 1));
            Assert.AreEqual("title", masterTemplate.GetPublishedContentTitle("mtcHeader", 1));
        }


        [TestMethod]
        public void GetContentHtmlAndTitle_AreaAndContentExists_Success()
        {
            MasterTemplate masterTemplate = new MasterTemplate();
            Content c = new Content();
            Locale locale = new Locale { ID = 1 };
            Administrator admin = new Administrator() { ID = 1 };
            MasterTemplateContentLink masterTemplateLink = new MasterTemplateContentLink
                                                               {
                                                                   Content = c, 
                                                                   MasterTemplate = masterTemplate, 
                                                                   AreaID = "mtcHeader"
                                                               };
            masterTemplate.Content.Add(masterTemplateLink);
            c.AddVersion(locale, 1001, "html 1", "title 1", admin);
            c.AddVersion(locale, 1002, "html 2", "title 2", admin);
            c.AddVersion(locale, 1003, "html 3", "title 3", admin);
            Assert.AreEqual("html 2", masterTemplate.GetContentHtml("mtcHeader", 1, "1002"));
            Assert.AreEqual("title 2", masterTemplate.GetContentTitle("mtcHeader", 1, "1002"));
        }


        [TestMethod]
        public void GetContent_AreaAndContentExists_Success()
        {
            MasterTemplate mt = new MasterTemplate();
            Content c = new Content { ID = 1 };
            MasterTemplateContentLink mtLink = new MasterTemplateContentLink { Content = c, MasterTemplate = mt, AreaID = "mtcMain" };
            mt.Content.Add(mtLink);
            Assert.AreEqual(1, mt.GetContent("mtcMain").ID);
        }


        [TestMethod]
        public void AddContentLink_NoPreviousContentInArea_Success()
        {
            MasterTemplate mt = new MasterTemplate();
            Content c = new Content { ID = 1 };
            mt.AddContentLink("mtcMain", c);
            Assert.AreSame(c, mt.GetContent("mtcMain"));
        }


        [TestMethod]
        public void AddContentLink_ExistingContentHasVersions_ExistingVersionsAreDeletedFromContentSet()
        {
            MasterTemplate mt = new MasterTemplate();
            Locale locale = new Locale { ID = 1 };
            Administrator admin = new Administrator();
            Content c = new Content { ID = 1 };
            long timestamp = DateTime.Now.Ticks;
            mt.AddContentLink("mtcMain", c);
            ContentTranslationVersion ctv = c.AddVersion(locale, timestamp, "1", "1", admin);
            mt.GetContentSet(locale).TranslationVersions.Add(ctv);
            ctv = c.AddVersion(locale, timestamp + 600000000, "1", "1", admin); // + 1 minute
            mt.GetContentSet(locale).TranslationVersions.Add(ctv);
            ctv = c.AddVersion(locale, timestamp + 1200000000, "1", "1", admin); // + 2 minutes
            mt.GetContentSet(locale).TranslationVersions.Add(ctv);
            Assert.AreEqual(3, mt.GetContentSet(locale).TranslationVersions.Count);
            // Now select another content
            Content c2 = new Content();
            mt.AddContentLink("mtcMain", c2);
            Assert.AreEqual(0, mt.GetContentSet(locale).TranslationVersions.Count);
        }


        [TestMethod]
        public void AddStylesheet()
        {
            MasterTemplate mt = new MasterTemplate();
            Stylesheet css1 = new Stylesheet();
            Stylesheet css2 = new Stylesheet();
            mt.AddStylesheet(css1);
            mt.AddStylesheet(css2);
            Assert.AreEqual(2, mt.AssociatedStylesheets.Count);
            Assert.AreEqual(1, mt.AssociatedStylesheets[1].SortIndex);
        }


        [TestMethod]
        public void InsertStylesheet()
        {
            MasterTemplate mt = new MasterTemplate();
            Stylesheet css1 = new Stylesheet();
            Stylesheet css2 = new Stylesheet();
            mt.AddStylesheet(css1);
            mt.InsertStylesheet(0, css2);
            Assert.AreEqual(2, mt.AssociatedStylesheets.Count);
            Assert.AreEqual(1, mt.AssociatedStylesheets[1].SortIndex);
            Assert.AreSame(css1, mt.AssociatedStylesheets[1].Stylesheet);
        }


        [TestMethod]
        public void RemoveStylesheet()
        {
            MasterTemplate mt = new MasterTemplate();
            Stylesheet css1 = new Stylesheet {ID = 1};
            Stylesheet css2 = new Stylesheet { ID = 2 };
            mt.AddStylesheet(css1);
            mt.AddStylesheet(css2);
            mt.RemoveStylesheet(css1);
            Assert.AreEqual(1, mt.AssociatedStylesheets.Count);
            Assert.AreSame(css2, mt.AssociatedStylesheets[0].Stylesheet);
        }


        [TestMethod]
        public void GetUnusedStylesheets()
        {
            Site site = new Site();
            MasterTemplate mt = new MasterTemplate {Site = site};
            Stylesheet css1 = new Stylesheet { ID = 1, Site = site };
            Stylesheet css2 = new Stylesheet { ID = 2, Site = site };
            Stylesheet css3 = new Stylesheet { ID = 3, Site = site };
            site.Stylesheets.Add(css1);
            site.Stylesheets.Add(css2);
            site.Stylesheets.Add(css3);
            mt.AddStylesheet(css1);
            mt.AddStylesheet(css2);
            Assert.AreEqual(2, mt.GetAssociatedStylesheetsSorted().Count);
            Assert.AreEqual(1, mt.GetUnusedStylesheets().Count);
            Assert.AreSame(css3, mt.GetUnusedStylesheets()[0]);
        }

    }

}
